<template>
  <div>
    <el-dialog
      :title="form.id ? '编辑菜单' : '添加菜单'"
      :visible.sync="isshow"
      @close="reset"
    >
      <el-form :model="form" :rules="rules" ref="menuForm">
        <el-form-item
          label="菜单名称"
          :label-width="formLabelWidth"
          prop="title"
        >
          <el-input
            clearable
            v-model="form.title"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="上级菜单" :label-width="formLabelWidth" prop="pid">
          <el-select
            @change="select"
            v-model="form.pid"
            placeholder="请选择上级菜单"
          >
            <el-option
              v-if="!form.id || form.pid == 0"
              label="顶级菜单"
              :value="0"
            ></el-option>
            <el-option
              v-show="!form.id || form.pid != 0"
              v-for="item in menuList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单类型" :label-width="formLabelWidth">
          <el-radio-group v-model="form.type">
            <el-radio :label="1" disabled>目录</el-radio>
            <el-radio :label="2" disabled>菜单</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          v-if="form.type == 1"
          label="菜单图标"
          :label-width="formLabelWidth"
        >
          <el-select v-model="form.icon" placeholder="请选择菜单图标">
            <el-option
              v-for="item in arrIcon"
              :key="item"
              :label="item"
              :value="item"
              ><i :class="item" :style="{ fontSize: '20px' }"></i
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          v-if="form.type == 2"
          label="菜单地址"
          :label-width="formLabelWidth"
        >
          <el-input clearable v-model="form.url" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="form.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="reset">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { menuinfo, menuaddedit } from "../../request/api";
import { mapActions, mapState } from "vuex";
export default {
  data() {
    return {
      isshow: false,
      dialogTableVisible: false,
      formLabelWidth: "120px",
      arrIcon: [
        "el-icon-goods",
        "el-icon-setting",
        "el-icon-user",
        "el-icon-shopping-bag-2",
      ],
      form: {
        pid: "",
        title: "",
        url: "",
        icon: "",
        type: "",
        status: 2,
      },
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        pid: [{ required: true, message: "请选择上级菜单", trigger: "blur" }],
      },
    };
  },
  computed: {
    ...mapState("menu", ["menuList"]),
  },
  methods: {
    ...mapActions("menu", ["menuAddEdit", "getMenuList"]),
    select(e) {
      this.form.type = e == 0 ? 1 : 2;
    },
    // 添加和编辑
    submitForm() {
      this.$refs.menuForm.validate((val) => {
        if (!val) {
          return;
        }
        let url = this.form.id ? "/menuedit" : "/menuadd";
        menuaddedit(url, this.form).then((res) => {
          if (res.code == 200) {
            this.$message.success(res.msg);
            this.getMenuList();
            this.reset();
            return;
          }
          this.$message.error(res.msg);
        });
      });
    },
    // 重置表单
    reset() {
      this.isshow = false;
      this.$refs.menuForm.resetFields();
      this.form = {
        pid: "",
        title: "",
        url: "",
        icon: "",
        type: "",
        status: 2,
      };
    },
    // 获取单条信息
    getInfo(id) {
      this.isshow = true;
      menuinfo({ id }).then((res) => {
        if (res.code == 200) {
          this.form = res.list;
          this.form.id = id;
        }
      });
    },
  },
};
</script>

<style scoped>
</style>